/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.webservice.client;

import com.edu.kamai.client.control.paperAnswerControl;
import com.edu.kamai.webservice.paperanswer.PaperAnswer;
import com.edu.kamai.webservice.paperanswer.PaperAnswerService;
import com.edu.kamai.webservice.paperanswer.PaperAnswerService_Service;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.soap.SOAPFaultException;

/**
 *
 * @author xyzhu
 */
public class WSPaperAnswerClient {
    private final QName qName = new QName("http://webservice.kamai.edu.com/",
            "paperAnswerService");
    private URL[] url;
    private int size;
    
    public WSPaperAnswerClient() throws Exception{
        size = paperAnswerControl.getNetList().size();
        url = new URL[size];
        for(int i=0;i<size;i++){
            url[i] = new URL("http://" + paperAnswerControl.getNetList().get(i) + 
                    "/paperAnswerService/paperAnswerService?WSDL");
        }
    }
    
    public int addPaperAnswer(int paperRole,String paperPath,int finishTime,
            int paperId,String userId,int contestId) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        do{
            try{
                System.out.println("addPaperAnswer : " + url[k]);
                PaperAnswerService_Service service = 
                        new PaperAnswerService_Service(url[k], 
                        qName);
                PaperAnswerService port = service.getPaperAnswerServicePort();
                res = port.addPaperAnswer(paperRole, paperPath, finishTime, 
                        paperId, userId, contestId);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
    }
    
    
    public List<PaperAnswer> checkPendingAnswer() throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        List<PaperAnswer> pending=null;
        do{
            try{
                System.out.println("checkPendingAnswer : " + url[k]);
                PaperAnswerService_Service service = 
                        new PaperAnswerService_Service(url[k], 
                        qName);
                PaperAnswerService port = service.getPaperAnswerServicePort();
                pending = port.getPendingAnswer();
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return pending;
    }
    
    public PaperAnswer getAnswerPaperByID(int aid) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        PaperAnswer pa=null;
        do{
            try{
                System.out.println("getAnswerPaperByID : " + url[k]);
                PaperAnswerService_Service service = 
                        new PaperAnswerService_Service(url[k], 
                        qName);
                PaperAnswerService port = service.getPaperAnswerServicePort();
                pa = port.getAnswerPaper(aid);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return pa;
    }
    
    public int updateScore(int aid,String teacherName,int score) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int ans = 0;
        do{
            try{
                System.out.println("updateScore : " + url[k]);
                PaperAnswerService_Service service = 
                        new PaperAnswerService_Service(url[k], 
                        qName);
                PaperAnswerService port = service.getPaperAnswerServicePort();
                ans = port.updateScore(aid, teacherName, score);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return ans;
    }
    
    public List<PaperAnswer> getAnswerScoreByContestIDAndUserID
            (int cid,String uid) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        List<PaperAnswer> ans = null;
        do{
            try{
                System.out.println("updateScore : " + url[k]);
                PaperAnswerService_Service service = 
                        new PaperAnswerService_Service(url[k], 
                        qName);
                PaperAnswerService port = service.getPaperAnswerServicePort();
                ans = port.getAnswerScoreByContestIDAndUserID(cid, uid);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return ans;
    }
    
}
